import 'dart:ffi';
import 'dart:ui';
import 'package:flutter/material.dart';

class CalculatorPage extends StatefulWidget {
  const CalculatorPage({super.key});

  @override
  State<StatefulWidget> createState() => CalculatorPageState();
}

class CalculatorPageState extends State<CalculatorPage> {
  TextEditingController txtCon = TextEditingController();
  List<Widget> childs = [];
  String typeStr = "";
  TextStyle txtStyle = const TextStyle(
    fontSize: 16,
    color: Color(0xff333333),
    fontWeight: FontWeight.bold,
  );

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    txtCon.text = "";

    childs.add(
      Container(
        height: 50,
        margin: const EdgeInsets.only(bottom: 20),
        child: TextField(
          controller: txtCon,
          enabled: false,
          style: const TextStyle(
            color: Color(0xff333333),
            fontSize: 30,
            fontWeight: FontWeight.bold,
          ),
          decoration: const InputDecoration(
            contentPadding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
            enabledBorder: OutlineInputBorder(
              borderRadius: BorderRadius.all(Radius.circular(8)),
              borderSide: BorderSide(
                color: Color(0xffe5e5e5),
                width: 1,
              ),
            ),
          ),
        ),
      ),
    );

    for (int i = 0; i < 10; i++) {
      childs.add(
        ElevatedButton(
          onPressed: () {
            clickNumber(i);
          },
          child: Text(i.toString(), style: txtStyle),
        ),
      );
    }

    List temps = [".", "+", "⁃", "*", "÷", "清空", "删除", "="];
    for (int i = 0; i < temps.length; i++) {
      childs.add(
        SizedBox(
          width: 85,
          child: ElevatedButton(
            onPressed: () {
              clickOther(temps[i]);
            },
            child: Text(temps[i], style: txtStyle),
          ),
        ),
      );
    }
  }

  clickOther(String title) {
    if (title == "删除") {
      txtCon.text = txtCon.text.substring(0, txtCon.text.length - 1);
      typeStr = txtCon.text.contains(typeStr) ? typeStr : "";
      setState(() {});
      return;
    } else if (title == "清空") {
      txtCon.text = "";
      typeStr = "";
      setState(() {});
      return;
    } else if (title == "=") {
      if (typeStr == "") {
        return;
      }

      List tempa = txtCon.text.split(typeStr);
      if (tempa.length < 2) {
        return;
      }

      double num1 = double.parse(tempa.first);
      double num2 = double.parse(tempa.last);
      switch (typeStr) {
        case "+":
          txtCon.text = (num1 + num2).toString();
        case "⁃":
          txtCon.text = (num1 - num2).toString();
        case "*":
          txtCon.text = (num1 == 0 || num2 == 0) ? "0" : "${num1 * num2}";
        case "÷":
          txtCon.text =
              (num1 == 0 || num2 == 0) ? "0" : (num1 / num2).toString();
      }

      if (txtCon.text.endsWith(".0")) {
        txtCon.text = txtCon.text.replaceAll(".0", "");
      }
      typeStr = ""; //计算完成后，将计算方式清空
      return;
    } else if (title == ".") {
      String value = txtCon.text;
      if (value == "") {
        return;
      }

      if (typeStr.isNotEmpty) {
        value = value.substring(value.indexOf(typeStr));
      }

      if (value.contains(".") || value == typeStr) {
        return;
      }
    } else {
      //计算方式
      if (txtCon.text == "") {
        //第一个数值为空
        return;
      } else if (typeStr.isNotEmpty) {
        //已设置计算方式
        return;
      }

      typeStr = title;
    }

    //将小数点和计算方式加入字符串
    txtCon.text += title;
    setState(() {});
  }

  clickNumber(int i) {
    String value = txtCon.text;
    if (typeStr.isNotEmpty) {
      //获取到计算方式后的数值
      value = value.substring(value.indexOf(typeStr) + 1);
    }

    //数值以0开头，不能再输入0
    if (value == "0") {
      return;
    }

    //将数值加在字符串后面
    txtCon.text += i.toString();
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: const Text("计算器"),
        backgroundColor: Colors.white,
        titleTextStyle: const TextStyle(
          color: Colors.black,
          fontSize: 18,
          fontWeight: FontWeight.bold,
        ),
      ),
      body: Container(
        padding: const EdgeInsets.all(10),
        child: Wrap(
          spacing: 10,
          alignment: WrapAlignment.center,
          children: childs,
        ),
      ),
    );
  }
}
